﻿using System;
using SmartSiteManagementSystem.IRepository.WorkTask;
using SmartSiteManagementSystem.Models.WorkTask;
using SmartSiteManagementSystem.Repository.DBHelper;
using System.Data;
using Dapper;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MySql.Data.MySqlClient;

namespace SmartSiteManagementSystem.Repository.WorkTask
{
    public class SupplyingPlaceMaterialInfoRepository : ISupplyingPlaceMaterialInfoRepository
    {
        #region dataTable
        /*
drop table if exists `supplying_place_material`;
create table `supplying_place_material`( 
	`id` int(11) not null AUTO_INCREMENT primary key, 
    `supplying_place_id` int NOT NULL,
    `material_count` int NOT NULL default 0,
	`create_time` datetime not null default now()
) default charset=utf8;
         */
        #endregion
        //private IDbConnection _readConn;
       // private IDbConnection _writeConn;

        public bool Delete(Hashtable param)
        {
            bool back = false;
            if (param == null)
                param = new Hashtable();
            String querySql = @"delete from supplying_place_material";
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where ");
                    int parCount = 0;
                    if (param.ContainsKey("id"))
                    {
                        pars.Add("id", param["id"]);

                        whereCondition.Append(" id =@id");
                        parCount++;
                    }
                    if (param.ContainsKey("supplying_place_id"))
                    {
                        pars.Add("supplying_place_id", param["supplying_place_id"]);

                        whereCondition.Append(" supplying_place_id =@supplying_place_id");
                        parCount++;
                    }
                }
                string execSql = querySql + whereCondition.ToString();
                int backInt = _writeConn.Execute(execSql, pars);
                if (backInt > 0)
                {
                    back = true;
                }
            }
            return back;
        }

        public SupplyingPlaceMaterialInfo Get(Hashtable param)
        {
            SupplyingPlaceMaterialInfo back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT id as Id, supplying_place_id as SupplyingPlaceId, material_count as MaterialCount,create_time as CreateTime FROM supplying_place_material";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("id"))
                    {
                        pars.Add("id", param["id"]);

                        whereCondition.Append(" id =@id");
                        
                    }
                    if (param.ContainsKey("supplying_place_id"))
                    {
                        pars.Add("supplying_place_id", param["supplying_place_id"]);

                        whereCondition.Append(" supplying_place_id =@supplying_place_id");
                       
                    }

                }
                whereCondition.Append(" limit 1");
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.QueryFirstOrDefault<SupplyingPlaceMaterialInfo>(execSql, pars);
            }
            return back;
        }

        public int GetCount(Hashtable param)
        {
            int back = 0;
            if (param == null)
                param = new Hashtable();
            String querySql = "select count(id) from supplying_place_material";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("id"))
                    {
                        pars.Add("id", param["id"]);

                        whereCondition.Append(" id =@id");

                    }
                    if (param.ContainsKey("supplying_place_id"))
                    {
                        pars.Add("supplying_place_id", param["supplying_place_id"]);

                        whereCondition.Append(" supplying_place_id =@supplying_place_id");

                    }
                }

                string execSql = querySql + whereCondition.ToString();
                #endregion
                back = _readConn.QueryFirstOrDefault<int>(execSql, pars);
            }
            return back;
        }

        public IList<SupplyingPlaceMaterialInfo> GetList(Hashtable param, string orderBy, int first, int rows)
        {

            IList<SupplyingPlaceMaterialInfo> back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT id as Id, supplying_place_id as SupplyingPlaceId, material_count as MaterialCount,create_time as CreateTime FROM supplying_place_material";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("id"))
                    {
                        pars.Add("id", param["id"]);

                        whereCondition.Append(" id =@id");

                    }
                    if (param.ContainsKey("supplying_place_id"))
                    {
                        pars.Add("supplying_place_id", param["supplying_place_id"]);

                        whereCondition.Append(" supplying_place_id =@supplying_place_id");

                    }
                }
                if (string.IsNullOrEmpty(orderBy))
                {
                    whereCondition.Append(" order by id desc");
                }
                else
                {
                    whereCondition.Append(" order by @orderBy");
                    pars.Add("orderBy", orderBy);
                }
                whereCondition.Append(" limit @first,@rows");
                pars.Add("first", first);
                pars.Add("rows", rows);
                #endregion
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.Query<SupplyingPlaceMaterialInfo>(execSql, pars).AsList<SupplyingPlaceMaterialInfo>();
            }
            return back;
        }

        public int Insert(SupplyingPlaceMaterialInfo obj)
        {
            int back = 0;
            #region sql
            string sql = @"INSERT INTO supplying_place_material
(
supplying_place_id,
material_count,
create_time
)
VALUES
(
@supplying_place_id,
@material_count,
@create_time);select last_insert_id();";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                back = _writeConn.QueryFirst<int>(sql, new
                {
                    supplying_place_id=obj.SupplyingPlaceId,
                    material_count=obj.MaterialCount,
                    create_time=obj.CreateTime
                });
            }
            return back;
        }

        public bool Update(SupplyingPlaceMaterialInfo obj)
        {
            bool back = false;
            #region sql
            string sql = @"update supplying_place_material
set
supplying_place_id=@supplying_place_id,
material_count=@material_count,
create_time=@create_time
WHERE id =@id;";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                int backint = _writeConn.Execute(sql, new
                {
                    id = obj.Id,
                    supplying_place_id = obj.SupplyingPlaceId,
                    material_count = obj.MaterialCount,
                    create_time = obj.CreateTime
                });
                if (backint > 0)
                {
                    back = true;
                }
            }
            return back;
        }
    }
}

